/*
 *	 Copyright [2010] Stanley Ding(Dingshengyu)
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 *   Missian is based on hessian, mina and spring. Any project who uses 
 *	 missian must agree to hessian, mima and spring's license.
 *	  Hessian: http://hessian.caucho.com/
 *    Mina:http://mina.apache.org
 *	  Spring(Optional):http://www.springsource.org/	 
 *
 *   @author stanley
 *	 @date 2010-11-28
 */
package com.missian.common.io;

import java.io.IOException;
import java.io.OutputStream;

import org.apache.mina.core.buffer.IoBuffer;

/**
 * 
 *
 */
public class IoBufferOutputStream extends OutputStream{
	private static final int DEFAULT_SIZE = 1024;
	private IoBuffer outputBuffer;
	
	public IoBufferOutputStream() {
		this(DEFAULT_SIZE);
	}
	public IoBufferOutputStream(int initBufferSize) {
		outputBuffer = IoBuffer.allocate(initBufferSize);
		outputBuffer.setAutoExpand(true);
	}
	public IoBufferOutputStream(IoBuffer outputBuffer) {
		this.outputBuffer = outputBuffer;
	}
	@Override
	public void write(int b) throws IOException {
		outputBuffer.put((byte)b);
	}
	@Override
	public void write(byte[] b, int off, int len) throws IOException {
		outputBuffer.put(b, off, len);
	}
	@Override
	public void write(byte[] b) throws IOException {
		outputBuffer.put(b);
	}
	public IoBuffer flip() {
		outputBuffer.flip();
		return outputBuffer;
	}
}